import request from '@/plugins/request'
import { IResponseData } from '@/types/request-base'
import { json2FormData } from '@/utils'

// 经销商变更单相关接口
const perCodes: any = {
  A: 'dealernewarchive', //'经销商档案新建',
  B: 'dealernewshop', //'经销商新增门店',
  C: 'dealershopchange', //'经销商新增业务',
  D: 'dealershopchange', //'经销商取消业务',
  E: 'dealerfreezeandthaw', //'经销商冻结',
  F: 'dealerfreezeandthaw', //'经销商解冻',
  G: 'dealershutupShop', //'经销商分店关店',
  H: 'dealerarchivechange', //'经销商档案变更',
  I: 'dealertermination' //'经销商合作终止'
}

/**
 * 分页查询
 * @param {object} data 参数
 */
export function getPage({ page, rows, sort, order, filtersRaw, sortersRaw, paramsRaw, pageFlag }: any) {
  // 排序：先进度后时间
  const defautlSorter: object[] = [
    { id: 'schedule', property: 'schedule', direction: 'ASC' },
    { id: 'createTime', property: 'createTime', direction: 'DESC' }
  ]
  const typeObj = filtersRaw.find((v: any) => v.property === 'type')
  let PERCODE = ''
  if (typeObj && typeObj.value) {
    const tmpStr = typeObj.value.replace('[', '').replace(']', '')
    const tmpArr = tmpStr.split(',')
    tmpArr.forEach((code: string) => {
      PERCODE = perCodes[code] || ''
    })
  }

  const params = {
    page,
    rows,
    sort: sort || '',
    order: order || 'DESC',
    pageFlag: pageFlag !== false ? true : false,
    onlyCountFlag: false,
    filtersRaw: JSON.stringify(filtersRaw),
    sortersRaw: JSON.stringify(sortersRaw && sortersRaw.length > 0 ? sortersRaw : defautlSorter),
    paramsRaw: JSON.stringify(paramsRaw)
  }
  const data = json2FormData(params)

  return request<IResponseData>({
    method: 'post',
    url: `/app/v1/ctmCustomerChange/page`,
    data,
    headers: {
      'Permission-PerCode': PERCODE,
      'Permission-App': 'CTM_APP'
    }
  })
}

/**
 * 单据详情
 * @param {string} ID
 */
export function getChangeDetail(id: string) {
  return request<IResponseData>({
    method: 'get',
    url: `/app/v1/ctmCustomerChange/${id}`
  })
}

/**
 * 单据终止
 * @param {string} ID
 */
export function stopChange(id: string) {
  return request<IResponseData>({
    method: 'POST',
    url: `/app/v1/ctmCustomerChange/${id}`,
    headers: {
      repeatSubmit: false
    }
  })
}

/**
 * 新增/保存单据
 * @param {string} ID
 * @param {string} type 单据类型（数据字典：CTM_CUS_ARCHIVE_CHANGE_TYPE）
 * @param {string} schedule 单据处理进度 (数据字典：CTM_CUS_ARCHIVE_CHANGE_PROGRESS：保存 A，提交：B)，新增是必须为A,提交时为B
 */
export function createSave(data: any) {
  return request<IResponseData>({
    method: data && data?.id ? 'PUT' : 'POST',
    url: `/app/v1/ctmCustomerChange`,
    data,
    loading: true,
    headers: {
      repeatSubmit: false
    }
  })
}

/**
 *新增或修改-合伙经营
 * @param {Array} data 列表数据
 * @param {String} changeCode 变更单编码
 */
export function savePartner(data: any, changeCode: string) {
  return request<IResponseData>({
    method: 'post',
    url: `/app/v1/ctmCustomerChange/insertOrUpdate/partner?changeCode=${changeCode}`,
    data,
    loading: true,
    headers: {
      repeatSubmit: false
    }
  })
}

/**
 *新增或修改-交接
 * @param {Array} data 列表数据
 * @param {String} changeCode 变更单编码
 */
export function saveHandover(data: any) {
  return request<IResponseData>({
    method: 'post',
    url: `/app/v1/ctmCustomerChange/insertOrUpdate/handover`,
    data,
    loading: true,
    headers: {
      repeatSubmit: false
    }
  })
}

/**
 * 保存门店信息
 * @param {string} ID
 */
export function saveShopInfo(data: any) {
  return request<IResponseData>({
    method: 'post',
    url: `/app/v1/ctmCustomerShopBusiness`,
    data,
    loading: true,
    headers: {
      repeatSubmit: false
    }
  })
}
/**
 * 批量保存门店信息
 * @param {string} ID
 */
export function batchSaveShopInfo(changeCode: string, data: any) {
  return request<IResponseData>({
    method: 'post',
    url: `/app/v1/ctmCustomerShopBusiness/listSave?changeCode=${changeCode}`,
    data,
    loading: true,
    headers: {
      repeatSubmit: false
    }
  })
}
/**
 * 根据变更单编码查询原商家门店信息
 * @param {String} changeCode 变更单编码
 */
export function queryShopByChangeCode(changeCode: string) {
  return request<IResponseData>({
    method: 'get',
    url: `/app/v1/ctmCustomerShopBusiness/listByChangeCode/${changeCode}`
  })
}

/**
 * 根据经销商编码批量查询门店信息
 * @param {Array} customerCodes 经销商编码集合
 */
export function queryShopByCustomerCode(customerCodes: any) {
  return request<IResponseData>({
    method: 'post',
    url: `/app/v1/ctmCustomerShopBusiness/listChangeOfCooperationEndByCustomerCodes`,
    data: { customerCodes }
  })
}

/**
 * 根据变更单编码查询新商接管的门店
 * @param {String} changeCode 变更单编码
 */
export function queryNewBusinessTakeoverShops(changeCode: string) {
  return request<IResponseData>({
    method: 'get',
    url: `/app/v1/ctmCustomerShopBusiness/listByChangCodeAndIsNewBusinessTakeover/${changeCode}`
  })
}
